package com.neymar.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neymar.entity.Applicant;
import com.neymar.entity.Position;
import com.neymar.entity.Recruiter;
import com.neymar.entity.User;
import com.neymar.mapper.PositionMapper;
import com.neymar.service.PositionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neymar.util.PageUtils;
import com.neymar.vo.PositionVO;
import com.neymar.vo.UserVO;
import lombok.Lombok;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 求职者表 服务实现类
 * </p>
 *
 * @author neymar
 * @since 2021-06-02
 */
@Service
public class PositionServiceImpl implements PositionService {

    @Autowired
    private PositionMapper positionMapper;

    @Override
    public int save(Position position) {
        return positionMapper.insert(position);
    }

    @Override
    public int deletePositionById(String positionId) {
        return positionMapper.deleteById(positionId);
    }

    @Override
    public int updatePositionById(Position position) {
        return positionMapper.updateById(position);
    }

    @Override
    public Position findPosition(Position position) {
        return positionMapper.selectById(position.getPositionId());
    }

    @Override
    public int positionNumber() {
        QueryWrapper<Position> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("count(*) as number");
        List<Map<String, Object>> maps = positionMapper.selectMaps(queryWrapper);
        return ((Number) maps.get(0).get("number")).intValue();
    }

    @Override
    public List showTypeInfo() {
        QueryWrapper<Position> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("position_type", "count(*) as number").groupBy("position_type");
        List<Map<String, Object>> maps = positionMapper.selectMaps(queryWrapper);
        return maps;
    }

    @Override
    public List showSalaryInfo() {
        QueryWrapper<Position> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("position_type", "count(case when position_salary<3500 then 1 else null end) '0-3500'",
                "count(case when position_salary>3500 and position_salary<=5000 then 2 else null end) '3501-5000'",
                "count(case when position_salary>5001 and position_salary<=7500 then 3 else null end) '5001-7500'",
                "count(case when position_salary>7501 and position_salary<=10000 then 4 else null end) '7501-10000'",
                "count(case when position_salary> 10000 then 5 else null end) '10000+'").groupBy("position_type");
        List<Map<String, Object>> maps = positionMapper.selectMaps(queryWrapper);
        return maps;
    }

    @Override
    public PositionVO findAllPosition(Integer current, Integer size, String type, String name) {
        // 分页查询
        IPage<Position> iPage = new Page<>();
        iPage.setCurrent(current);
        iPage.setSize(size);
        QueryWrapper<Position> wrapper = new QueryWrapper<>();
        wrapper.like("position_type", type);
        wrapper.like("position", name);
        iPage = positionMapper.selectPage(iPage, wrapper);

        PositionVO positionVO = new PositionVO();
        positionVO.setCurrent(current);
        positionVO.setSize(size);
        positionVO.setPages(iPage.getPages());
        positionVO.setTotal(iPage.getTotal());
        positionVO.setPositions(iPage.getRecords());
        return positionVO;
    }

    @Override
    public PositionVO findAllPositionDetails(Integer current, Integer size, String type, String name) {

        // 获取记录
        List<Map<String, String>> allDetails = positionMapper.findAllPositionsDetails(current, size, type, name);

        PositionVO positionVO = new PositionVO();
        positionVO.setCurrent(current);
        positionVO.setSize(size);
        positionVO.setPositionsDetails(allDetails);

        // 获取总的页数
        Long total = (long) allDetails.size();
        Long page = PageUtils.getPage(total, size);
        positionVO.setPages(page);
        positionVO.setTotal(total);
        return positionVO;
    }
}
